package util

import (
	"encoding/json"
	"github.com/EDDYCJY/go-gin-example/pkg/e"
	"github.com/EDDYCJY/go-gin-example/pkg/http_call"
	"github.com/EDDYCJY/go-gin-example/pkg/setting"
	"github.com/EDDYCJY/go-gin-example/pkg/substrate"
	"runtime"
)

type HttpApiRes struct {
	Errcode int
	Errmsg  string
	Data    interface{}
}

func PostRequest(api string, params []byte) *HttpApiRes {
	api = setting.ServerSetting.ClientNode + api
	res, err := http_call.Post(api, params)
	if err != nil {
		return nil
	}
	data := HttpApiRes{}
	err = json.Unmarshal([]byte(res), &data)
	if err != nil {
		return nil
	}

	return &data
}

func GetRequest(api string) *HttpApiRes {
	api = setting.ServerSetting.ClientNode + api
	res, err := http_call.Get(api)
	if err != nil {
		return nil
	}
	data := HttpApiRes{}
	err = json.Unmarshal([]byte(res), &data)
	if err != nil {
		return nil
	}

	return &data
}

func GenWalletSign(wallet_addr string, private_key string) (map[string]string, e.FResult) {
	if runtime.GOOS == "linux" && runtime.GOARCH == "amd64" {
		s_wallet := wallet_addr
		s_prvkey := private_key
		s_nonce := GenNonce()
		s_sign := substrate.Priv_sign(s_prvkey, s_nonce)

		params := map[string]string{
			"nonce":  s_nonce,
			"sign":   s_sign,
			"wallet": s_wallet,
		}

		return params, e.FResult_OK
	} else {
		params := map[string]string{
			"session_id":      "5vrBp28rfM3WemvvBRRfuP27ZjeHfYrXPi92xieSgzmr4A4H3RcC1uQ",
			"session_id_sign": "1086fd675c3115e6f3ea3d78c4f241180f7e7fdd177b12971c0724f016ebe2663d81f034c060c2ec96cf3bb2c401e0c271d540964d209990704516cecdbf3c8f",
		}

		return params, e.FResult_OK
	}
}
